<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>异常机制 | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/25.a995a755.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础高级</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/0c7696/" class="sidebar-link">集合框架</a></li><li><a href="/pages/19e70b/" aria-current="page" class="active sidebar-link">异常机制</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/19e70b/#一、今日学习内容和目标" class="sidebar-link">一、今日学习内容和目标</a></li><li class="sidebar-sub-header level2"><a href="/pages/19e70b/#二、异常概述和处理机制-理解" class="sidebar-link">二、异常概述和处理机制（理解）</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/19e70b/#_2-1-异常-exception-概述" class="sidebar-link">2.1 异常 (Exception) 概述</a></li><li class="sidebar-sub-header level3"><a href="/pages/19e70b/#_2-2-异常处理机制" class="sidebar-link">2.2 异常处理机制</a></li><li class="sidebar-sub-header level3"><a href="/pages/19e70b/#_2-3-异常处理-掌握" class="sidebar-link">2.3 异常处理(掌握)</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_2-3-1-try-catch-掌握" class="sidebar-link">2.3.1 try...catch(掌握)</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_2-3-2-多重catch-掌握" class="sidebar-link">2.3.2 多重catch(掌握)</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_2-3-3-异常对象-掌握" class="sidebar-link">2.3.3 异常对象(掌握)</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_2-3-4-try-catch-finally-掌握" class="sidebar-link">2.3.4 try...catch...finally(掌握)</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/19e70b/#三、抛出异常-掌握" class="sidebar-link">三、抛出异常(掌握)</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/19e70b/#_3-1-throws-掌握" class="sidebar-link">3.1 throws (掌握)</a></li><li class="sidebar-sub-header level3"><a href="/pages/19e70b/#_3-2-throw-掌握" class="sidebar-link">3.2 throw (掌握)</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/19e70b/#四、异常分类-掌握" class="sidebar-link">四、异常分类（掌握）</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/19e70b/#_4-1-异常的继承体系-掌握" class="sidebar-link">4.1 异常的继承体系（掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/19e70b/#_4-2-error-了解" class="sidebar-link">4.2 Error（了解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/19e70b/#_4-3-exception-掌握" class="sidebar-link">4.3 Exception（掌握）</a></li><li class="sidebar-sub-header level5"><a href="/pages/19e70b/#_4-3-1-运行时异常-了解" class="sidebar-link">4.3.1 运行时异常（了解）</a></li><li class="sidebar-sub-header level5"><a href="/pages/19e70b/#_4-3-2-编译时异常-了解" class="sidebar-link">4.3.2 编译时异常（了解）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/19e70b/#五、自定义异常-掌握" class="sidebar-link">五、自定义异常(掌握)</a></li><li class="sidebar-sub-header level2"><a href="/pages/19e70b/#六、小结与作业" class="sidebar-link">六、小结与作业</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/19e70b/#_6-1-小结" class="sidebar-link">6.1 小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/19e70b/#_6-2-作业" class="sidebar-link">6.2 作业</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_6-2-1-题目1" class="sidebar-link">6.2.1 题目1</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_6-2-2-题目2" class="sidebar-link">6.2.2 题目2</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_6-2-3-题目3" class="sidebar-link">6.2.3 题目3</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_6-2-4-题目4" class="sidebar-link">6.2.4 题目4</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_6-2-5-题目5" class="sidebar-link">6.2.5 题目5</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_6-2-6-题目6" class="sidebar-link">6.2.6 题目6</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_6-2-7-题目7" class="sidebar-link">6.2.7 题目7</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_6-2-8-题目8" class="sidebar-link">6.2.8 题目8</a></li><li class="sidebar-sub-header level4"><a href="/pages/19e70b/#_6-2-9-题目9" class="sidebar-link">6.2.9 题目9</a></li></ul></li></ul></li><li><a href="/pages/a53030/" class="sidebar-link">多线程</a></li><li><a href="/pages/16b819/" class="sidebar-link">IO</a></li><li><a href="/pages/258730/" class="sidebar-link">单元测试与配置文件</a></li><li><a href="/pages/1fdc1c/" class="sidebar-link">反射与内省机制</a></li><li><a href="/pages/80d6e3/" class="sidebar-link">Lambda表达式</a></li><li><a href="/pages/a53dcc/" class="sidebar-link">Stream流操作</a></li><li><a href="/pages/42948c/" class="sidebar-link">注解</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E5%9F%BA%E7%A1%80%E9%AB%98%E7%BA%A7" title="分类" data-v-06225672>基础高级</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-03</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">异常机制<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="异常机制"><a href="#异常机制" class="header-anchor">#</a> 异常机制</h1> <h2 id="一、今日学习内容和目标"><a href="#一、今日学习内容和目标" class="header-anchor">#</a> 一、今日学习内容和目标</h2> <ul><li>了解异常概念和异常处理机制的作用</li> <li>掌握异常处理
<ul><li>try-catch</li> <li>try-catch-finally</li></ul></li> <li>掌握抛出异常的两种方式
<ul><li>throws</li> <li>throw</li></ul></li> <li>掌握异常分类 ( 面试题 )
<ul><li>异常继承体系</li> <li>运行时异常和编译时异常</li></ul></li> <li>掌握自定义异常</li></ul> <h2 id="二、异常概述和处理机制-理解"><a href="#二、异常概述和处理机制-理解" class="header-anchor">#</a> 二、异常概述和处理机制（理解）</h2> <h3 id="_2-1-异常-exception-概述"><a href="#_2-1-异常-exception-概述" class="header-anchor">#</a> 2.1 异常 (Exception) 概述</h3> <p><strong>异常</strong>就是程序在运行时出现的意外的，不正常的情况。</p> <p>若异常产生后没有正确的处理，会导致<strong>程序的中断</strong>，程序不继续执行,以致造成损失。</p> <img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/01-exception.png" alt="image-20201027125905055"> <p>结论：异常出现后，如果没有正确的处理，程序会崩溃</p> <h3 id="_2-2-异常处理机制"><a href="#_2-2-异常处理机制" class="header-anchor">#</a> 2.2 异常处理机制</h3> <p>​		所以我们在开发中要一套机制来处理各种可能会发生的异常，并对其作出正确的处理，确保程序的正常执行。这种机制称为<strong>异常处理机制</strong>，java语言是最先提供异常处理机制的。</p> <img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/01-exception2.png" alt="image-20201027130137940"> <p>异常处理机制作用</p> <ul><li><strong>引导程序向正确的方向运行</strong>，不至于崩溃</li></ul> <h3 id="_2-3-异常处理-掌握"><a href="#_2-3-异常处理-掌握" class="header-anchor">#</a> 2.3 异常处理(掌握)</h3> <p>java异常处理包含两种代码块。一种是try...catch,一种是try...catch...finally.</p> <h4 id="_2-3-1-try-catch-掌握"><a href="#_2-3-1-try-catch-掌握" class="header-anchor">#</a> 2.3.1 try...catch(掌握)</h4> <p>把有可能产生异常的代码放到try中，如果产生异常由catch代码块处理。语法</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">try</span> <span class="token punctuation">{</span>  
    <span class="token comment">// 可能产生异常的代码块</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span><span class="token punctuation">(</span>异常类型 e<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
    <span class="token comment">// 异常处理方案</span>
<span class="token punctuation">}</span>
<span class="token comment">// 后面如果还有代码，无论try中的代码有没有异常，这里都会继续执行</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>try...catch执行有三种情况 :</p> <p>情况1：没有发生异常，正常执行，catch代码块不会执行</p> <p><strong>情况2</strong>：出现异常，catch代码块捕获异常，并进行异常处理。处理完成后程序继续执行。</p> <img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/03-exception3.png" alt="img"> <p>需求1 : 从控制台输入两个数并做除法</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Scanner</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TryCatchDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Scanner</span> sc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;请输入第一个操作数:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> num1 <span class="token operator">=</span> sc<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;请输入第二个操作数:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> num2 <span class="token operator">=</span> sc<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">int</span> r <span class="token operator">=</span> num1 <span class="token operator">/</span> num2<span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;r = &quot;</span> <span class="token operator">+</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment">// 负责处理异常</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;出现异常了&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;程序正常结束&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><p>异常发生后，从异常发生的那句代码开始，程序不继续向下运行，立即转入异常处理。</p> <blockquote><p>异常捕获后处理的过程：
step 1: 程序直接跳到catch处进行异常对象的捕获
step 2: 捕获后通过()中的异常类型进行异常对象的类型匹配
step 3: 类型匹配后，进入对应的{} 进行异常处理</p></blockquote> <p>需求2：定义一个两个整数的除法方法，在方法中打印除法结果，并在方法中进行异常处理</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CatchDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;begin&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">//divide方法会出现错误，但是因为divide方法已经处理，不会影响到调用方法的继续向下执行。</span>
		<span class="token function">divide</span><span class="token punctuation">(</span><span class="token number">17</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;ending&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//能够执行到ending</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">divide</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">try</span> <span class="token punctuation">{</span>
			<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a <span class="token operator">/</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">ArithmeticException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>  <span class="token comment">//捕获ArithmeticException类型的异常</span>
			<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;除法运算数有错误&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><strong>情况3</strong>：异常类型不匹配</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/03-exception4.png" alt=""></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">InputMismatchException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Scanner</span></span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test01</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Scanner</span> sc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;请输入第一个操作数:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> num1 <span class="token operator">=</span> sc<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;请输入第二个操作数:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> num2 <span class="token operator">=</span> sc<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">int</span> r <span class="token operator">=</span> num1 <span class="token operator">/</span> num2<span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;r = &quot;</span> <span class="token operator">+</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InputMismatchException</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment">// 负责处理异常</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;出现输入不匹配异常，然后处理&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;程序结束&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><p>在输入第二个操作数时输入0，异常不匹配，程序中断，此时我们就需要多重catch了。</p> <h4 id="_2-3-2-多重catch-掌握"><a href="#_2-3-2-多重catch-掌握" class="header-anchor">#</a> 2.3.2 多重catch(掌握)</h4> <p>可以为try代码书写多个catch用于捕获多个具体类型的异常。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">try</span> <span class="token punctuation">{</span>
	<span class="token comment">//可能出现异常的代码</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span><span class="token punctuation">(</span>异常类型<span class="token class-name">A</span> 变量<span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token comment">//处理A类型异常的代码</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span><span class="token punctuation">(</span>异常类型<span class="token class-name">B</span> 变量<span class="token punctuation">)</span><span class="token punctuation">{</span>	
	<span class="token comment">//处理B类型异常的代码</span>
<span class="token punctuation">}</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>多重catch异常类型匹配过程：从上而下一个一个匹配，先匹配类型A，在匹配类型B，以此类推</p> <p>书写时 : 具体子类异常在上，父类异常类型在下。</p> <p>需求：定义一个方法，传入两个字符串类型的数值，在方法中打印数值的商，并在方法中进行异常处理</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">InputMismatchException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Scanner</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MutiCatchDemo</span> <span class="token punctuation">{</span>
  <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		 <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;begin&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		 <span class="token function">divide</span><span class="token punctuation">(</span><span class="token string">&quot;17&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;0&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		 <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;ending&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">divide</span><span class="token punctuation">(</span><span class="token class-name">String</span> a<span class="token punctuation">,</span> <span class="token class-name">String</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> y <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>x <span class="token operator">/</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">NumberFormatException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//处理数字格式化异常的代码</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;字符串不包含可解析的整数&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">ArithmeticException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//处理算术异常的代码</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;除数不能为0&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//处理其他未知的异常</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;未知异常&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h4 id="_2-3-3-异常对象-掌握"><a href="#_2-3-3-异常对象-掌握" class="header-anchor">#</a> 2.3.3 异常对象(掌握)</h4> <p>异常对象</p> <ul><li>在哪里被创建：出现异常时的那条语句产生的</li> <li>谁创建的：通过Exception或其具体子类创建，大多数情况下都是由其子类创建</li></ul> <p>注意：<strong>Exception是所有异常类的父类</strong>。</p> <p>异常对象的常见方法：</p> <table><thead><tr><th>常见方法</th> <th>方法介绍</th></tr></thead> <tbody><tr><td>toString</td> <td>返回异常类型和异常信息</td></tr> <tr><td>getMessage</td> <td>返回异常信息</td></tr> <tr><td>printStackTrace</td> <td>打印堆栈信息(红色)。包含了异常信息，异常类型，异常位置，方便程序开发阶段的调试（一般要打开），也是JVM默认的异常处理机制</td></tr></tbody></table> <h4 id="_2-3-4-try-catch-finally-掌握"><a href="#_2-3-4-try-catch-finally-掌握" class="header-anchor">#</a> 2.3.4 try...catch...finally(掌握)</h4> <p>try...catch 和之前一样用于捕获并处理异常</p> <p>作用 : finally代码块用于处理异常后的收尾工作</p> <ul><li>收尾工作包含释放内存、<strong>关闭文件、关闭网络连接、关闭数据库</strong>、关闭Xxx...</li></ul> <p><strong>特点：不管是否发生异常，finally总执行</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>step <span class="token number">1</span><span class="token operator">:</span> 打开文件
step <span class="token number">2</span><span class="token operator">:</span> 操作文件
step <span class="token number">3</span><span class="token operator">:</span> 关闭文件
    
<span class="token keyword">try</span> <span class="token punctuation">{</span>
    step <span class="token number">1</span><span class="token operator">:</span> 打开文件
	step <span class="token number">2</span><span class="token operator">:</span> 操作文件
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span> <span class="token class-name">Exception</span> e <span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 处理异常</span>
<span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
    step <span class="token number">3</span><span class="token operator">:</span> 关闭文件
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TryCatchFinallyDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Scanner</span> sc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
       <span class="token keyword">try</span> <span class="token punctuation">{</span>
           <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;请输入被除数:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	
           <span class="token keyword">int</span> num1 <span class="token operator">=</span> sc<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

           <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;请输入除数:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
           <span class="token keyword">int</span> num2 <span class="token operator">=</span> sc<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

           <span class="token keyword">int</span> r <span class="token operator">=</span> num1 <span class="token operator">/</span> num2<span class="token punctuation">;</span>
           <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span>

       <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
           e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
           <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我是finally&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">}</span>

		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;程序正常结束！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><p>存在return的try...catch...finally</p> <p>需求：定义一个方法，传入两个整数，返回两个整数的商，在方法中使用try-catch-finally代码块处理异常</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TryCatchFinallyDemo</span> <span class="token punctuation">{</span>
	
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">div</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span><span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> result <span class="token operator">=</span> a <span class="token operator">/</span> b<span class="token punctuation">;</span>
            <span class="token keyword">return</span> result<span class="token punctuation">;</span>
		<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
			<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;finally代码块&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>	
	<span class="token punctuation">}</span>
	
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token function">div</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;程序正常结束！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>注意：存在return的try...catch...finally块，finally先执行，然后执行return，<strong>return 总是最后执行的</strong>。</p> <h2 id="三、抛出异常-掌握"><a href="#三、抛出异常-掌握" class="header-anchor">#</a> 三、抛出异常(掌握)</h2> <p>一旦出现异常，程序会立即终止，所以在开发中我们一定要处理异常，处理异常有两种方式：</p> <p>一种是直接使用try-catch(-finally)处理异常（已讲），一种是自身抛出异常，不处理，而抛出异常，有两种：</p> <ul><li>方法里面可能会产生异常，自身不想处理，使用throws关键字<strong>提醒</strong>调用该方法的方法做处理。</li> <li>方法里面会出现异常，但方法不想处理这个异常，使用throw关键字<strong>抛出异常对象</strong>。</li></ul> <h3 id="_3-1-throws-掌握"><a href="#_3-1-throws-掌握" class="header-anchor">#</a> 3.1 throws (掌握)</h3> <p>throws 关键字用于声明一个方法可以抛出的异常</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token punctuation">[</span>修饰符<span class="token punctuation">]</span> 返回值类型 方法名<span class="token punctuation">(</span>参数列表<span class="token punctuation">)</span> <span class="token keyword">throws</span> 异常类型<span class="token class-name">A</span><span class="token punctuation">,</span>异常类型<span class="token class-name">B</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">{</span>
    
<span class="token punctuation">}</span>
举例 <span class="token operator">:</span> 
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ArrayIndexOutOfBoundsExcetpion</span><span class="token punctuation">,</span><span class="token class-name">NullPointerException</span> <span class="token punctuation">{</span> 
    <span class="token comment">// 方法体</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>声明异常的作用 :</p> <ul><li>表示告知调用者当前的方法可能会出现某些异常,使用时需要注意哦!!!</li> <li>如果当前方法没有出现任何异常, 那么代码会正常执行</li> <li>如果当前方法中出现了异常 , 会把异常交给本方法调用者处理</li></ul> <p>需求1：定义一个打印数组元素的方法，方法声明抛出异常</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ThrowsDemo</span> <span class="token punctuation">{</span>
   <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">printArr</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">NullPointerException</span> <span class="token punctuation">{</span>
       <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
           <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">}</span>
       <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>声明抛出异常的原因：printArr方法自身没有处理该异常，只能使用throws提醒该方法的使用者(main方法)进行处理异常。当然作为使用者也有两种处理方式 （<strong>要么try...catch ,要么也throws</strong>）：</p> <ul><li>要么自己捕获并处理</li> <li>要么再次声明 (异常继续上抛)</li></ul> <p>需求2：异常继续上抛</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ThrowsDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">printArr</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">NullPointerException</span> <span class="token punctuation">{</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> ele <span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>ele <span class="token operator">+</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">NullPointerException</span> <span class="token punctuation">{</span>
		<span class="token comment">// 调用printArr方法时，调用者处理不了异常，调用者再次抛出异常</span>
		<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token class-name">ThrowsDemo</span><span class="token punctuation">.</span><span class="token function">printArr</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>注意：调用带有异常声明的方法时，应该遵循以下原则：能在调用处明确处理优先处理，否则继续上抛。</p> <h3 id="_3-2-throw-掌握"><a href="#_3-2-throw-掌握" class="header-anchor">#</a> 3.2 throw (掌握)</h3> <p>throw关键字用于在方法内手动抛出一个具体的异常对象给调用者。语法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token punctuation">[</span>修饰符<span class="token punctuation">]</span> 返回值类型 方法名<span class="token punctuation">(</span>参数列表<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">XxException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 下面代码不会执行</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>注意 :</p> <ul><li>抛出异常对象的格式必须在方法的内部完成</li> <li>如果手动抛出一个异常对象，下面的代码无法执行（因为出现异常了）</li></ul> <p>抛出异常对象存在的意义 :</p> <ul><li>在方法中，当传递的参数有误，没有继续运行下去的意义了，则采取抛出处理，表示让该方法结束运行。</li> <li>告诉调用者方法内部出现了问题</li></ul> <p>练习  : 定义一个方法 , 方法的参数接收一个数组 , 在方法中遍历数组，对比两种方式：</p> <ul><li>如果方法接收的数组为null  , 使用打印输出语句提示</li> <li>如果方法接收的数组为null  , 使用抛出异常对象解决</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ThrowDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">printArr</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 如果方法接收的数组为null  , 使用输出语句提示</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;arr不能为null&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> ele <span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>ele <span class="token operator">+</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">printArr2</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 如果方法接收的数组为null  , 使用抛出异常解决</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">NullPointerException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> ele <span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>ele <span class="token operator">+</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">33</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token class-name">ThrowDemo</span><span class="token punctuation">.</span><span class="token function">printArr2</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><p><strong>throws和throw的区别</strong></p> <p>throws 用于方法声明上，表示该方法可能存在某种类型异常，也在提醒该方法调用者需要处理(该类型的)异常。</p> <p>throw 用在方法体内，用于抛出具体异常类的对象给调用者使用。</p> <h2 id="四、异常分类-掌握"><a href="#四、异常分类-掌握" class="header-anchor">#</a> 四、异常分类（掌握）</h2> <h3 id="_4-1-异常的继承体系-掌握"><a href="#_4-1-异常的继承体系-掌握" class="header-anchor">#</a> 4.1 异常的继承体系（掌握）</h3> <img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/06-exception-category.png" style="zoom:80%;"> <p>Throwable类有两个子类Error和Exception，分别表示错误和异常。</p> <p>Exception 和Error的子类大都是以Error或Exception作为类名后缀。</p> <h3 id="_4-2-error-了解"><a href="#_4-2-error-了解" class="header-anchor">#</a> 4.2 Error（了解）</h3> <p>Error，表示代码运行时 JVM（Java 虚拟机）出现的问题。如系统崩溃或内存溢出等，不需要处理Error，</p> <p>常见的Error：</p> <ul><li><p>StackOverflowError：当应用程序递归太深而发生堆栈溢出时，抛出该错误。比如死循环或者没有出口的递归调用。</p></li> <li><p>OutOfMemoryError：因为内存溢出或没有可用的内存提供给垃圾回收器时，Java 虚拟机无法分配一个对象，这时抛出该错误。比如new了非常庞大数量的对象而没释放。</p></li></ul> <h3 id="_4-3-exception-掌握"><a href="#_4-3-exception-掌握" class="header-anchor">#</a> 4.3 Exception（掌握）</h3> <p>​		Exception，表示程序在运行时出现的一些不正常情况，一般大多数表示轻度到中度的问题，属于可预测、可恢复问题。如除数为0，数组索引越界等，这种情况下，程序员通过合理的异常处理，确保程序的正常运行直到结束，常见的Exception</p> <ul><li><p>ArrayIndexOutOfBoundsException：用非法索引访问数组时抛出的异常。如果索引为负或大于等于数组大小，则该索引为非法索引。</p></li> <li><p>NullPointerException：当应用程序试图在需要对象的地方使用 null 时，抛出该异常。这种情况包括：</p></li></ul> <p>异常体系分成：checked（编译时）异常和runtime（运行时）异常。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/exception-extends.png" alt=""></strong></p> <p>划分规则是，<strong>RuntimeException和其子类属于运行时异常</strong>，异常除了运行时异常，其他都是编译时异常。</p> <h5 id="_4-3-1-运行时异常-了解"><a href="#_4-3-1-运行时异常-了解" class="header-anchor">#</a> 4.3.1 运行时异常（了解）</h5> <p>​		RuntimeException（运行时异常），顾名思义在编译时期不被检测，只有在运行时期才会被检查出来。</p> <p>RuntimeException（运行时异常）通常是指因设计或实现方式不当而导致的问题。程序员小心谨慎是可以避免的异常。如：事先判断对象是否为null就可以避免NullPointerException异常，事先检查除数不为0就可以避免ArithmeticException异常。</p> <p><strong>运行时异常特点：</strong></p> <ul><li>在编译阶段，Java编译器检查不出来</li> <li><strong>程序可处理(使用try-catch或throws处理)，可不处理</strong></li></ul> <h5 id="_4-3-2-编译时异常-了解"><a href="#_4-3-2-编译时异常-了解" class="header-anchor">#</a> 4.3.2 编译时异常（了解）</h5> <p>​		编译时异常，顾名思义就是在编译时期就会被检测到的异常。除了RuntimeException以及子类以外，其他的Exception及其子类都是编译时异常，有时候也称之为<strong>检查时异常</strong>。</p> <p><strong>编译时异常特点：</strong></p> <ul><li>在编译阶段，Java编译器会检查出异常</li> <li><strong>程序要求必须处理编译异常，使用try-catch或throws处理</strong>。</li></ul> <h2 id="五、自定义异常-掌握"><a href="#五、自定义异常-掌握" class="header-anchor">#</a> 五、自定义异常(掌握)</h2> <p>一个异常类只表示某一种特定的异常类型，在项目开发中，可能会出现特定的逻辑问题，此时开发者可以对这些问题进行封装成异常。比如说未来我们会定义一个LogicException用于表示业务逻辑异常。</p> <p>自定义异常的两种方式，<strong>可以继承Exception类或RuntimeException类</strong>。<strong>一般推荐继承RuntimeException类</strong>。</p> <p>自定义异常的步骤：</p> <p>(1) 确定异常类型（编译时异常、<strong>运行时异常</strong>）。</p> <p>(2) 继承异常的父类(编译时异常继承Exception、<strong>运行时异常继承RuntimeException</strong>)</p> <p>(3) 定义构造方法。继承异常类之后，一般的，需要提供无参构造方法和带一个String类型参数的构造器。</p> <p>需求：定义一个年龄的异常，当年龄值不在[1,100]这个范围，程序抛出年龄异常。</p> <p>自定义异常，继承RuntimeException</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AgeException</span> <span class="token keyword">extends</span> <span class="token class-name">RuntimeException</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token class-name">AgeException</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">AgeException</span><span class="token punctuation">(</span><span class="token class-name">String</span> message<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 通过父类的构造器把形参message赋值给父类的 detailMessage 字段</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>定义一个学生类</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
     <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
     <span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>
 
     <span class="token comment">// 省略构造器</span>
     <span class="token comment">// 省略name字段的设置器和访问器</span>
     <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
         <span class="token keyword">return</span> age<span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
 
     <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">AgeException</span><span class="token punctuation">{</span>
         <span class="token keyword">if</span><span class="token punctuation">(</span> age <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token operator">||</span> age <span class="token operator">&gt;</span> <span class="token number">100</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
             <span class="token class-name">AgeException</span> ex <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AgeException</span><span class="token punctuation">(</span><span class="token string">&quot;年龄值不能小于0或者不能大于120&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
             <span class="token keyword">throw</span> ex<span class="token punctuation">;</span>
         <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
             <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
         <span class="token punctuation">}</span>
     <span class="token punctuation">}</span>
 <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>测试</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CustomExceptionDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Student</span> s1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">&quot;will&quot;</span><span class="token punctuation">,</span><span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            s1<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span><span class="token number">130</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">AgeException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>本节快捷键</p> <p>try-catch / try-catch-finally 快捷键 =&gt; Ctrl + Alt + T</p> <p>ctrl + shift + 减号/加号  方法的折叠和展开</p> <h2 id="六、小结与作业"><a href="#六、小结与作业" class="header-anchor">#</a> 六、小结与作业</h2> <h3 id="_6-1-小结"><a href="#_6-1-小结" class="header-anchor">#</a> 6.1 小结</h3> <ul><li>掌握异常概念与异常处理机制</li> <li>掌握异常具体用回复-try-catch-finally</li> <li>掌握异常外抛技巧-throws+throw</li> <li>掌握异常继承体系与分类</li> <li>掌握自定义异常方式</li></ul> <h3 id="_6-2-作业"><a href="#_6-2-作业" class="header-anchor">#</a> 6.2 作业</h3> <h4 id="_6-2-1-题目1"><a href="#_6-2-1-题目1" class="header-anchor">#</a> 6.2.1 题目1</h4> <p>需求：调用Arrays和Collections工具类中的常用方法</p> <p>作业目的：了解Arrays和Collections工具类中的常用方法</p> <h4 id="_6-2-2-题目2"><a href="#_6-2-2-题目2" class="header-anchor">#</a> 6.2.2 题目2</h4> <p>需求：演示捕获单个异常 try-catch的使用</p> <p>作业目的：掌握try-catch的语法和代码的运行顺序</p> <h4 id="_6-2-3-题目3"><a href="#_6-2-3-题目3" class="header-anchor">#</a> 6.2.3 题目3</h4> <p>需求：演示捕获多个异常 try-catch-catch的使用</p> <p>作业目的：掌握try-catch-catch的语法和代码的运行顺序</p> <h4 id="_6-2-4-题目4"><a href="#_6-2-4-题目4" class="header-anchor">#</a> 6.2.4 题目4</h4> <p>需求：演示捕获多个异常 try-catch-finally的使用</p> <p>作业目的：掌握try-catch-finally的语法和代码的运行顺序</p> <h4 id="_6-2-5-题目5"><a href="#_6-2-5-题目5" class="header-anchor">#</a> 6.2.5 题目5</h4> <p>需求：演示返回异常throws的使用</p> <p>作业目的：掌握throws关键字的语法和作用</p> <h4 id="_6-2-6-题目6"><a href="#_6-2-6-题目6" class="header-anchor">#</a> 6.2.6 题目6</h4> <p>需求：演示返回异常throw的使用</p> <p>作业目的：掌握throw关键字的语法和作用</p> <h4 id="_6-2-7-题目7"><a href="#_6-2-7-题目7" class="header-anchor">#</a> 6.2.7 题目7</h4> <p>需求：自定义定义一个年龄的异常，当年龄值不在[1,100]这个范围，程序抛出年龄异常。</p> <p>作业目的：掌握自定义异常的步骤</p> <h4 id="_6-2-8-题目8"><a href="#_6-2-8-题目8" class="header-anchor">#</a> 6.2.8 题目8</h4> <p>现有三个自定义异常年龄低异常（<strong>LowAgeException</strong>）、年龄高异常(<strong>HighAgeException</strong>)、工资高异常（<strong>HighSalaryException</strong>）</p> <p>他们都是运行时异常，并且都在自己的构造器中调用了父类的构造器:</p> <p>public RuntimeException(String message);</p> <p>编写一个员工类：</p> <ol><li><p>成员变量：
编号，姓名，年龄，工资，身份证号码，员工人数</p></li> <li><p>构造器：
构造器：设置编号，年龄，姓名；</p> <p>如果年龄小于18，抛出<strong>LowAgeException</strong>，如果年龄大于65抛出<strong>HighAgeException</strong>，如果姓名为null或为空字符串，抛出空指针异常。</p></li> <li><p>方法
增加工资  addSalary(double addSalary)，声明抛出工资高异常，当增加的工资大于5000时，抛出此异常。</p></li></ol> <h4 id="_6-2-9-题目9"><a href="#_6-2-9-题目9" class="header-anchor">#</a> 6.2.9 题目9</h4> <p>建立银行账户类（Account）</p> <ul><li>定义成员变量double balance表示存款，</li> <li>Account类的构造方法能初始化账户余额</li> <li>Account类中有取款的方法withDrawal(double dAmount)
<ul><li>当取款的数额大于存款时，抛出自定义的<strong>InsufficientFundsException</strong></li> <li>当取款数额为负数，抛出自定义的NagativeFundsException。</li></ul></li></ul> <p>如创建一个账户对象 Account account = new Account(100)，表示当前余额100元</p> <ul><li>当调用方法withDrawal(150) 抛出自定义异常InsufficientFundsException</li> <li>当调用方法withDrawal(-15)时会抛出自定义异常NagativeFundsException</li></ul></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=exception" title="标签">#exception</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/0c7696/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">集合框架</div></a> <a href="/pages/a53030/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">多线程</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/0c7696/" class="prev">集合框架</a></span> <span class="next"><a href="/pages/a53030/">多线程</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/25.a995a755.js" defer></script>
  </body>
</html>
